<?php
namespace scripts;

use globalunit\model\AdminModel;
use QYS\Protocol\Response;
use QYS\Protocol\Request;
use QYS\Task\Factory;
use QYS\Db\Mysql;
use QYS\Util\Debug;
use QYS\Log\Log;
use QYS\Db\Redis;

use globalunit\utils\CoroutineCurl;
use QYS\Core\Config as QYSConfig;
use QYS\QYS;


class AdminAccess
{
    public function access($request,$response,$params)
    {
        $response->addHeader("Content-Type", 'application/json');
        $response->addHeader("Access-Control-Allow-Origin", "*");
        $response->addHeader("Access-Control-Allow-Methods", 'POST, GET, OPTIONS, DELETE');
        $response->addHeader('Access-Control-Allow-Headers', "token,Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
        $response->sendHttpHeader();
        if ($request->getRequest()->server['request_method'] == 'OPTIONS') {
            $response->say("");
            $response->status(200);
            QYS::bye();
            return;
        }
        $debug = QYSConfig::get("debug");
        $cookie = $request->getRequest()->header["authorization"];
        if(!$debug){
            $cookie = $request->getRequest()->cookie;
            $strs = [];
            foreach ($cookie as $key => $value) {
                array_push($strs, $key."=".$value);
            }

            $cookie = implode(";", $strs);
        }
        if(empty($cookie)){
            Log::var_dump(["len"=>35]);
            $errcode = 401;
            $response->say(json_encode(array("errcode" => $errcode, "errmsg" => "验证失败, 请稍后再试!")));
            QYS::bye();
            return;
        }

        $url = QYSConfig::get("user_url");
        $res = CoroutineCurl::request($url, "/api/user", $cookie);

        Log::var_dump(["res"=>$res]);

        if(empty($res)){
            Log::var_dump(["len"=>44]);
            $errcode = 401;
            $response->say(json_encode(array("errcode" => $errcode, "errmsg" => "系统内部错误, 请稍后再试!")));
            QYS::bye();
            return;
        }

        if(!$res["success"]){
            Log::var_dump(["len"=>52]);
            $errcode = 10000;
            $response->say(json_encode(array("errcode" => $errcode, "errmsg" => $res["errormsg"])));
            QYS::bye();
            return;
        }

        $m = new AdminModel();
        $ok = $m->get("user_id = '{$res["data"]["userId"]}'");
        if(!$ok){
            $m->user_id = $res["data"]["userId"]??"";
            $m->username = $res["data"]["userName"]??"";
            $m->phone = $res["data"]["userPhone"]??"";
            $m->pfid = $res["data"]["userPfid"]??"";
            $m->organize_id = $res["data"]["userOrganize"]??"";
            $m->organize_name = $res["data"]["organizeName"]??"";
            $m->avatar = $res["data"]["avatar"]??"";
            $ok = $m->insert();
            if (!$ok){
                Log::var_dump(["len"=>71]);
                $errcode = 10001;
                $response->say(json_encode(array("errcode" => $errcode, "errmsg" => "系统繁忙,请稍后再试!")));
                QYS::bye();
                return;
            }
        }
        $request->getRequest()->get["user_id"] = $res["data"]["userId"];
        $request->getRequest()->get["organize_id"] = $res["data"]["userOrganize"];
        $request->getRequest()->get["pfid"] = $res["data"]["userPfid"];
    }

}
